package com.db.domain;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.mk.type.MySQLTypeFactory;
import org.mk.type.Type;

import com.db.ColumnInfoBean;
import com.excel.read.Config;
import com.excel.read.DBTable;
import com.operation.OperationMap;
import com.operation.TreeTool;
import com.service.ImportService;
import com.xml.ExtractFactory;
import com.xml.RecordExtract;

/**
 * 
 * @author zhanghua
 * 
 */
public class Communication implements java.io.Serializable {

	private TreeTool treeTool;

	public long id;
	
	private OperationMap operationMap;
	
	/**
	 * 
	 */
	public String request;
	public String response;

	public String flag;

	public String created;

	public String type;
	// 请求方的店
	public long clientFrom;
	// 处理请求的店，该店一定存在请求方所要的记录，也就是Record.storeFrom
	public long clientTo;

	public long storeId;

	public long recordId;

	/**
	 * 请求之间的依赖将形成一棵树
	 */
	public long parent;

	public List<Communication> children = new ArrayList<Communication>();
	public Communication parentNode;

	/**
	 * 层次结构
	 */
	public long level;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getCreated() {
		return created;
	}

	public void setCreated(String created) {
		this.created = created;
	}

	public long getStoreId() {
		return storeId;
	}

	public void setStoreId(long storeId) {
		this.storeId = storeId;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public long getClientFrom() {
		return clientFrom;
	}

	public void setClientFrom(long clientFrom) {
		this.clientFrom = clientFrom;
	}

	public long getClientTo() {
		return clientTo;
	}

	public void setClientTo(long clientTo) {
		this.clientTo = clientTo;
	}

	public String getRequest() {
		return request;
	}

	public void setRequest(String request) {
		this.request = request;
	}

	public String getResponse() {
		return response;
	}

	public void setResponse(String response) {
		this.response = response;
	}

	public void setParent(Communication parentNode) {
		this.parentNode = parentNode;
		if (parentNode != null) {
			parentNode.children.add(this);
		}

	}

	public boolean hasChild() {
		if (this.children.size() == 0) {
			return false;
		}
		return true;
	}

	public boolean ready() {
		return this.response != null && !this.response.equalsIgnoreCase("");
	}

	
	
	
	/**
	 * 供root使用
	 * @param aCommunication
	 * @return 需要响应的Communication
	 * @throws Exception
	 * Sep 2, 2010
	 */
	public List<Communication> readyAll() throws Exception {
		Communication aCommunication = this;
		//如果还没有响应
		if(!aCommunication.ready()) {
			return null;
		}		
		List<Communication> list = new ArrayList<Communication>();		
		OperationMap aOperationMap2 = aCommunication.getOperationMap();
		TreeTool aTreeTool = aCommunication.getTreeTool();
		// 有孩子节点
		if (aCommunication.hasChild()) {
			// 取外键
			list = aOperationMap2.getRequest(aCommunication);
			//aTreeTool.addReq(list);
			
			// 继续
			for (Communication bCommunication : aCommunication.children) {
				bCommunication.readyAll();
			}
		} else {
			// 叶子节点
			list = aOperationMap2.getRequest(aCommunication);
			aTreeTool.addReq(list);
		}

		return list;
	}

	/**
	 * 必须是PO
	 * 
	 * @return
	 * @throws Exception
	 *             Aug 25, 2010
	 */
	public String getNameId() throws Exception {		
		RecordExtract aRecordExtract = new RecordExtract();
		OperationMap aOperationMap = null;
		int opType = ImportService.PO_FLAG;
		if (!ExtractFactory.isPO(response)) {
			opType = ImportService.SQL_FLAG;

		} else {
			aOperationMap = (OperationMap) aRecordExtract.extractOperationMap(
					response, 0);
		}
		// 执行SQL语句
		Object value = null;
		DBTable dbTable = (DBTable) Config.allTable
				.get(aOperationMap.opTableName.toLowerCase());
		ColumnInfoBean pk = dbTable.getPk();
		String fieldName = pk.getName().toLowerCase();
		Type type = MySQLTypeFactory.basic(pk.getNoPresitionTypeName());
		Map request = aOperationMap.record.fieldMap;
		Object fieldValue = request.get(fieldName.toUpperCase());
		if (fieldValue == null) {
			value = null;
		} else {
			value = type.getValue((String) fieldValue);
		}

		return dbTable.getName() + "_" +value;
	}
	
	

	public TreeTool getTreeTool() {
		return treeTool;
	}

	public void setTreeTool(TreeTool treeTool) {
		this.treeTool = treeTool;
	}

	public long getParent() {
		return parent;
	}

	public OperationMap getOperationMap() {
		return operationMap;
	}

	public void setOperationMap(OperationMap operationMap) {
		this.operationMap = operationMap;
	}
	
	
	public void convertOperationMap() {
		if(!this.ready()) {
			return;
		}
		RecordExtract aRecordExtract = new RecordExtract();
		OperationMap aOperationMap = null;
		try {
			aOperationMap = (OperationMap) aRecordExtract.extractOperationMap(response, clientFrom);
		} catch (Exception e) {			
			e.printStackTrace();
		}
		this.operationMap = aOperationMap;
		
	}
	

}
